import java.util.*;

public class Demo5 {
    public static Queue<int[]> q = new LinkedList<>();
    public static int[] dx = {0, 0, 1, -1};
    public static int[] dy = {1, -1, 0, 0};
    public static int n;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        n = scan.nextInt();
        int[][] arr1 = new int[n][n];
        int[][] arr2 = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr1[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr2[i][j] = scan.nextInt();
            }
        }

        boolean[][] flag1 = new boolean[n][n];
        boolean[][] flag2 = new boolean[n][n];

        List<Integer> list1 = new ArrayList<>();//arr1中与边界相邻的区域
        List<Integer> list2 = new ArrayList<>();//arr1中不与边界相邻的区域
        List<Integer> list3 = new ArrayList<>();//arr2中与边界相邻的区域
        List<Integer> list4 = new ArrayList<>();//arr2中不与边界相邻的区域

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr1[i][j] == 1 && !flag1[i][j]) {
                    int area = bfs(i, j, flag1, arr1);
                    if (!flag1[i][j]) {
                        list1.add(area);
                    } else {
                        list2.add(area);
                    }
                    flag1[i][j] = true;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr2[i][j] == 1 && !flag2[i][j]) {
                    int area = bfs(i, j, flag2, arr2);
                    if (!flag1[i][j]) {
                        list3.add(area);
                    } else {
                        list4.add(area);
                    }
                    flag2[i][j] = true;
                }
            }
        }
        Collections.sort(list1, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        Collections.sort(list2, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        Collections.sort(list3, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        Collections.sort(list4, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        System.out.println(list1.toString());
        System.out.println(list2.toString());
        System.out.println(list3.toString());
        System.out.println(list4.toString());

        int ret = 0;
        if (list1.size() == 0) {
            if (list3.size() == 0) {
                ret = Math.max(list2.get(0) + list4.get(0), ret);
            } else if (list4.size() == 0){
                ret = Math.max(list2.get(0) + list3.get(0), ret);
            }
        } else {
            if (list3.size() == 0) {
                ret = Math.max(list1.get(0) + list4.get(0), ret);
            } else if (list4.size() == 0){
                ret = Math.max(list1.get(0) + list3.get(0), ret);
            }
        }
        System.out.println(ret);

        scan.close();
    }

    //返回区域面积
    public static int bfs(int i, int j, boolean[][] flag, int[][] arr) {
        q.offer(new int[]{i, j});
        flag[i][j] = true;
        int ret = 1;
        boolean flag1 = false;//标记这片区域是否与边界相连
        while (!q.isEmpty()) {
            int[] arr1 = q.poll();
            int x = arr1[0];
            int y = arr1[1];
            if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
                flag1 = true;
            }
            flag[x][y] = true;
            for (int k = 0; k < 4; k++) {
                int xx = x + dx[k];
                int yy = y + dy[k];
                if (xx >= 0 && xx < n && yy >= 0 && yy < n && !flag[xx][yy] && arr[xx][yy] == 1) {
                    q.offer(new int[]{xx, yy});
                    ret++;
                    flag[xx][yy] = true;
                    if (xx == 0 || xx == n - 1 || yy == 0 || yy == n - 1) {
                        flag1 = true;
                    }
                }
            }
        }

        //使用标记数组的初始位置来判断标记这片区域是否与边界相连
        if (flag1) {
            flag[i][j] = false;
        }

        return ret;
    }
}
